﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T8task2
{
    class DBHelper
    {
        public static string Sqlconn = "server=.;database=GameBoxDB;Integrated Security=true";
        //连接状态
        public static SqlConnection conn = null;

        //连接数据库
        public static void InitConnection()
        {
            //连接为空
            if (conn == null)
            {
                conn = new SqlConnection(Sqlconn);
            }
            //连接断开
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            //连接中断
            if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();

            }
        }
        //断开式连接
        public static DataSet DT(string sql)
        {
            InitConnection();
            //定义DataAdapter对象
            SqlDataAdapter dar = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            dar.Fill(ds);
            conn.Close();
            return ds;
        }
    }
}
